﻿Public Class frmCalibrationMgr

    Friend HasChanged As Boolean = False

    Private Sub frmCalibrationManage_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        For Each tbl As clsTable In OpenTables
            If tbl.TableType = clsTable.enumTableType.Calibration Then cboCalibrationName.Items.Add(tbl.TableName)
        Next
        cboTimeAgg.Items.AddRange(New String() {"None", "1 Hour", "1 Day", "1 Week", "1 Month"})
        cboTimeAgg.SelectedIndex = -1
        cboPercentiles.Items.AddRange(New String() {"5% and 95%", "10% and 90%", "20% and 80%", "25% and 75%"})
        cboPercentiles.SelectedIndex = -1
        cboCalcMethod.Items.AddRange(New String() {"Use all values in each of Measured and Simulated series", "Use interpolated Simulated values paired with Measured values"})
        cboCalibrationName.SelectedIndex = IIf(cboCalibrationName.Items.Count = 0, -1, 0)
    End Sub

    Private Sub cboCalibrationName_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboCalibrationName.SelectedIndexChanged
        Dim tf As Boolean = cboCalibrationName.SelectedIndex <> -1
        btnEdit.Enabled = tf
        btnDelete.Enabled = tf
        cboTimeAgg.Enabled = tf
        cboPercentiles.Enabled = tf
        cboCalcMethod.Enabled = tf
        grpSeries.Enabled = tf
        btnStats.Enabled = tf
        If tf Then
            With CalibrationTable(cboCalibrationName.Text).Calibration
                If .tblMeasured IsNot Nothing Then txtTblMeasured.Text = .tblMeasured.ToString
                If .tblSimulated IsNot Nothing Then txtTblSimulated.Text = .tblSimulated.ToString
                txtNumBranches.Text = .dictBranches.Values.Count
                cboTimeAgg.SelectedIndex = .TimeAggregation
                cboPercentiles.SelectedIndex = .PreferredPercentiles
                cboCalcMethod.SelectedIndex = .CalcMethod
                chkAutoPrefix.Checked = .AutoSeriesPrefix
                txtPrefix.Text = .SeriesPrefix
                txtSuffixMeas.Text = .LegendSuffixMeasured
                txtSuffixSim.Text = .LegendSuffixSimulated
                txtSeriesFormatting.Text = String.Format("{0} series format(s) defined; {1} series groups are defined", .SeriesFormats.Count, .SeriesGroups.Count)
            End With
            grpSeries.Enabled = True
        Else
            txtTblMeasured.Text = ""
            txtTblSimulated.Text = ""
            txtNumBranches.Text = ""
            cboTimeAgg.SelectedIndex = -1
            cboPercentiles.SelectedIndex = -1
            grpSeries.Enabled = False
            txtSeriesFormatting.Text = ""
        End If
    End Sub

    Private Sub cboTimeAgg_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboTimeAgg.SelectedIndexChanged
        If cboTimeAgg.SelectedIndex = -1 Then Exit Sub
        With CalibrationTable(cboCalibrationName.Text).Calibration
            .TimeAggregation = cboTimeAgg.SelectedIndex
        End With
    End Sub

    Private Sub cboPercentiles_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboPercentiles.SelectedIndexChanged
        If cboPercentiles.SelectedIndex = -1 Then Exit Sub
        With CalibrationTable(cboCalibrationName.Text).Calibration
            .PreferredPercentiles = cboPercentiles.SelectedIndex
        End With
    End Sub

    Private Sub cboCalcMethod_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboCalcMethod.SelectedIndexChanged
        If cboCalcMethod.SelectedIndex = -1 Then Exit Sub
        With CalibrationTable(cboCalibrationName.Text).Calibration
            .CalcMethod = cboCalcMethod.SelectedIndex
        End With
    End Sub

    Private Sub cbo_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboTimeAgg.SelectionChangeCommitted, cboPercentiles.SelectionChangeCommitted, cboCalcMethod.SelectionChangeCommitted
        HasChanged = True
    End Sub

    Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
        Dim frm As New frmCalibration(CalibrationTable(cboCalibrationName.Text).Calibration.Clone)
        With frm
            .Text &= " - " & cboCalibrationName.Text
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                CalibrationTable(cboCalibrationName.Text).Calibration = .Calibration
                cboCalibrationName.SelectedIndex = -1
                cboCalibrationName.Text = .Calibration.Name
                FixSeriesOrder()
                HasChanged = True
            End If
            .Dispose()
        End With
    End Sub

    Private Sub btnNew_Click(sender As System.Object, e As System.EventArgs) Handles btnNew.Click
        Dim NumCal As Integer = 0
        For Each tbl As clsTable In OpenTables
            If tbl.TableType = clsTable.enumTableType.Calibration Then NumCal += 1
        Next
        Dim NewName As String = InputBox("Enter the name that will be used to identify the new calibration dataset:", "New Calibration Dataset", "Calibration " & NumCal + 1)
        If NewName = "" Then Exit Sub
        If cboCalibrationName.Items.Contains(NewName) Then WarningMsg("The connection name you entered ({0}) already exists; please enter a new name.", NewName) : Exit Sub
        txtTblMeasured.Text = ""
        txtTblSimulated.Text = ""
        txtNumBranches.Text = ""
        txtSeriesFormatting.Text = ""

        With New frmCalibration(New clsCalibration(NewName))
            .Text &= " - " & NewName
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                Dim tbl As New clsTable
                tbl.CreateCalibrationTable(.Calibration)
                OpenTables.Add(tbl)
                cboCalibrationName.Items.Add(NewName)
                cboCalibrationName.Text = NewName
            End If
        End With

    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        If cboCalibrationName.SelectedIndex <> -1 AndAlso WarningMsg("Are you sure you want to delete all settings for this calibration?") = Windows.Forms.DialogResult.OK Then
            OpenTables.Remove(CalibrationTable(cboCalibrationName.Text))
            cboCalibrationName.Items.Remove(cboCalibrationName.Text)
            cboCalibrationName.SelectedIndex = -1
            txtTblMeasured.Text = ""
            txtTblSimulated.Text = ""
            txtNumBranches.Text = ""
            cboTimeAgg.SelectedIndex = -1
            cboCalibrationName.SelectedIndex = IIf(cboCalibrationName.Items.Count = 0, -1, 0)
            txtSeriesFormatting.Text = ""
        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub chkAutoPrefix_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkAutoPrefix.CheckedChanged
        CalibrationTable(cboCalibrationName.Text).Calibration.AutoSeriesPrefix = chkAutoPrefix.Checked
        txtPrefix.Enabled = Not chkAutoPrefix.Checked
    End Sub

    Private Sub txtPrefix_Validated(sender As Object, e As System.EventArgs) Handles txtPrefix.Validated
        CalibrationTable(cboCalibrationName.Text).Calibration.SeriesPrefix = txtPrefix.Text
    End Sub

    Private Sub txtSuffixMeas_Validated(sender As Object, e As System.EventArgs) Handles txtSuffixMeas.Validated
        CalibrationTable(cboCalibrationName.Text).Calibration.LegendSuffixMeasured = txtSuffixMeas.Text
    End Sub

    Private Sub txtSuffixSim_Validated(sender As Object, e As System.EventArgs) Handles txtSuffixSim.Validated
        CalibrationTable(cboCalibrationName.Text).Calibration.LegendSuffixSimulated = txtSuffixSim.Text
    End Sub

    Private Sub btnStats_Click(sender As System.Object, e As System.EventArgs) Handles btnStats.Click
        With New frmCalibrationRpt
            .cboCalibration.Text = cboCalibrationName.Text
            .DefaultStations = CalibrationTable(cboCalibrationName.Text).Calibration.DefaultStations
            .DefaultParameters = CalibrationTable(cboCalibrationName.Text).Calibration.DefaultParameters
            .ShowDialog(Me)
            CalibrationTable(cboCalibrationName.Text).Calibration.DefaultStations = .cboStation.Text
            CalibrationTable(cboCalibrationName.Text).Calibration.DefaultParameters = .cboParameter.Text
            .Dispose()
        End With
    End Sub

    Private Sub FixSeriesOrder()
        'the series order setting may have changed; for each tab, find calibration series and fix the series order
        For i As Integer = 0 To GraphForm.TabGraph.TabCount - 2
            With ActiveGraphTab(i)
                For j As Integer = .Series.Count - 1 To 0 Step -1
                    Dim ser As clsSeries = .Series(j)
                    If ser.Table IsNot Nothing AndAlso
                        ser.Table.TableType = clsTable.enumTableType.Calibration AndAlso
                        ser.ZedCurve IsNot Nothing AndAlso
                        ser.ZedCurveMeas IsNot Nothing Then

                        With .Zed.GraphPane.CurveList
                            .Remove(ser.ZedCurveMeas)
                            Dim idx As Integer = .IndexOf(ser.ZedCurve)
                            'insert before or after depending on desired series order
                            .Insert(idx + Choose(ser.Table.Calibration.SeriesOrder + 1, 0, 1), ser.ZedCurveMeas)
                        End With

                    End If
                Next
                .ResetYAxes()
                .Zed.AxisChange()
                .Zed.Refresh()
            End With
        Next
    End Sub

    Private Sub txtTblMeasured_KeyDown(sender As Object, e As KeyEventArgs) Handles txtTblMeasured.KeyDown, txtTblSimulated.KeyDown
        HasChanged = True
    End Sub

    Private Sub chkAutoPrefix_Click(sender As Object, e As EventArgs) Handles chkAutoPrefix.Click
        HasChanged = True
    End Sub
End Class